package cn.mdmm.service.impl.system;

import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.mdmm.entity.system.SysRole;
import cn.mdmm.entity.system.SysRoleMenu;
import cn.mdmm.entity.system.SysRoleUser;
import cn.mdmm.mapper.system.SysRoleMapper;
import cn.mdmm.mapper.system.SysRoleMenuMapper;
import cn.mdmm.mapper.system.SysRoleUserMapper;
import cn.mdmm.service.system.SysMenuService;
import cn.mdmm.service.system.SysRoleService;
import cn.modoumama.service.base.impl.BaseServiceImpl;

@Service
@Transactional
public class SysRoleServiceImpl extends BaseServiceImpl<SysRole, Long> implements SysRoleService {
	//最新时间（增删改）
	private static Date date = null;
	/** 角色与权限，角色Id-权限结合*/
	private static Map<Long, Set<String>> rolePowers;
	private static Map<Long, SysRole> allRoles;
	@Autowired
	private SysRoleMenuMapper sRoleMenuMapper;
	@Autowired
	private SysRoleUserMapper sRoleUserMapper;	
	@Autowired
	private SysRoleMapper sRoleMapper;
	@Autowired
	private SysMenuService SysMenuService;

	@Autowired
	public void setMapper(SysRoleMapper mapper) {
		setGenericMapper(mapper);
	}

	@PostConstruct
	@Override
	public void init() {
		//获取菜单权限
		Map<Long, String> allMenuPowers = SysMenuService.getAllMenuPowers();
		date = new Date();
		Map<String, Object> cMap = new HashMap<>();
		allRoles = new HashMap<>();
		List<SysRoleMenu> allRoleMenus = sRoleMenuMapper.findModelsByCondition(cMap);
		cMap.put("allRoleType", 1);
		List<SysRole> allRole = findModelsByCondition(cMap);
		rolePowers = new HashMap<Long, Set<String>>();
		for (SysRole role : allRole) {
			Set<String> powers = new HashSet<>();
			rolePowers.put(role.getRoleId(), powers);
			allRoles.put(role.getRoleId(), role);
		}
		
		for (SysRoleMenu sysRoleMenu : allRoleMenus) {
			Long roleId = sysRoleMenu.getRoleId();
			Long menuId = sysRoleMenu.getMenuId();
			if(allMenuPowers.get(menuId) != null){
				rolePowers.get(roleId).add(allMenuPowers.get(menuId));
			}
			
		}
	}
	
	@Override
	public Map<Long, Set<String>> getRolePowers() {
		//菜单有更新，更新角色与权限
		Map<Long, String> allMenuPowers = SysMenuService.getAllMenuPowers(date);
		if(allMenuPowers != null){
			init();
		}
		return rolePowers;
	}
	
	@Override
	public Set<String> getRolePowers(Long roleId) {
		//菜单有更新，更新角色与权限
		Map<Long, String> allMenuPowers = SysMenuService.getAllMenuPowers(date);
		if(allMenuPowers != null){
			init();
		}
		return rolePowers.get(roleId);
	}

	/**
	 * 获取角色
	 * @return
	 */
	public SysRole getRole(Long roleId){
		return allRoles.get(roleId);
	}
	@Override
	public List<SysRoleMenu> selectRoleMenuByCondition(Long roleId) {
		Map<String, Object> condititon = new HashMap<String, Object>();
		condititon.put("roleId", roleId);
		return sRoleMenuMapper.findModelsByCondition(condititon);
	}

	
	@Override
	public List<SysRoleUser> selectSUserRoleByCondition(Long roleId) {
		Map<String, Object> condititon = new HashMap<String, Object>();
		condititon.put("roleId", roleId);
		return sRoleUserMapper.findModelsByCondition(condititon);
	}
		
	public List<SysRole> selectRolesByUserId(Long userId){
		return sRoleMapper.selectRolesByUserId(userId);
	}
	
	
    public void  bindRoleMenu(Long roleId,String[] menuIds){
    	Map<String, Object> condition = new HashMap<String, Object>();
		condition.put("roleId", roleId);
		sRoleMenuMapper.deleteByCondition(condition);
		for(int i=0;i<menuIds.length;i++){
			SysRoleMenu record = new SysRoleMenu();
			record.setRoleId(roleId);
			record.setMenuId(Long.valueOf(menuIds[i]));
			sRoleMenuMapper.insertModel(record);
		}
		init();
    }
	
	public void  bindRoleUser(Long roleId,String[] userIds){
		Map<String, Object> condition = new HashMap<String, Object>();
		condition.put("roleId", roleId);
		sRoleUserMapper.deleteByCondition(condition);
		for(int i=0;i<userIds.length;i++){
			SysRoleUser record = new SysRoleUser();
			record.setRoleId(roleId);
			record.setUserId(Long.valueOf(userIds[i]));
			sRoleUserMapper.insertModel(record);
		}
	}
	
	public  void  deleteRoleByIds(String[] roleIds){
		for(String roleId : roleIds){
			//删除角色
			removeById(Long.parseLong(roleId));	
			Map<String, Object> condition = new HashMap<String, Object>();
			condition.put("roleId", roleId);					
			//删除角色和菜单的绑定关系
			sRoleMenuMapper.deleteByCondition(condition);
			//删除角色和用户的绑定关系
			sRoleUserMapper.deleteByCondition(condition);
		}
		init();
	}
}
